package com.sky.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sky.dto.DishDTO;
import com.sky.entity.DishEntity;
import com.sky.entity.DishFlavorEntity;
import com.sky.entity.SetmealDishEntity;
import com.sky.entity.SetmealEntity;
import com.sky.enumeration.DishStatusEnum;
import com.sky.enumeration.SetmealStatusEnum;
import com.sky.exceptionHandler.ServiceException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private SetmealMapper setmealMapper;


    @Transactional
    @Override
    public void saveWithFlavor(DishDTO dishDTO) {
        DishEntity dishEntity = new DishEntity();
        BeanUtils.copyProperties(dishDTO, dishEntity);
        //  向菜品表添加1条数据
        dishMapper.save(dishEntity);

        // 获取自增的主键id
        Long id = dishEntity.getId();

        // 向口味表添加n条数据
        List<DishFlavorEntity> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size() > 0) {
            flavors.forEach((item) -> {
                item.setDishId(id);
            });
            dishFlavorMapper.saveBatch(flavors);
        }

    }

    @Override
    public List<DishVO> page(Integer page, Integer size, DishStatusEnum statusEnum, Integer categoryId, String name) {
        if (page == null) {
            page = 1;
        }
        if (size == null) {
            size = 10;
        }
        PageHelper.startPage(page, size);
        List<DishVO> list = dishMapper.select(statusEnum, categoryId, name);
        PageInfo<DishVO> pageInfo = new PageInfo<>(list);
        return pageInfo.getList();
    }

    @Override
    public List<DishVO> getByCategoryId(Integer categoryId) {
        List<DishVO> list = dishMapper.select(null, categoryId, "");
        return list;
    }

    @Override
    public DishVO getById(Integer id) {
        DishEntity dishEntity = dishMapper.getById(id);
        List<DishFlavorEntity> dishFlavors = dishFlavorMapper.getByDishId(id);
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dishEntity, dishVO);
        dishVO.setFlavors(dishFlavors);
        return dishVO;
    }

    @Override
    @Transactional
    public void update(DishDTO dto) {
        DishEntity entity = new DishEntity();
        BeanUtils.copyProperties(dto, entity);
        // 更新dish表
        dishMapper.update(entity);

        // 删除对应的口味
        dishFlavorMapper.delByDishId(entity.getId());

        // 重新插入对应的口味
        List<DishFlavorEntity> flavors = dto.getFlavors();
        if (flavors != null && flavors.size() > 0) {
            dishFlavorMapper.saveBatch(flavors);
        }
    }

    @Override
    public void enableOrDisable(DishStatusEnum statusEnum, Long id) {
        DishEntity entity = DishEntity.builder().id(id).status(statusEnum).build();
        dishMapper.update(entity);
        if (statusEnum.equals(DishStatusEnum.Disable)) {
            // 查询当前菜品是否关联着套餐
            SetmealDishEntity setmealDish = setmealDishMapper.getByDishId(id);
            if (setmealDish != null) {
                // 如果关联着套餐，将套餐也设置为停售状态
                SetmealEntity setmealEntity = SetmealEntity.builder().id(setmealDish.getSetmealId()).status(SetmealStatusEnum.Disable).build();
                setmealMapper.update(setmealEntity);
            }
        }
    }

    @Override
    @Transactional
    public void delBatch(List<Long> ids) {
        List<DishEntity> dishEntityList = dishMapper.selectIds(ids);
        // 1. 起售中的菜品不可删除;
        dishEntityList.forEach(dish -> {
            if (dish.getStatus().equals(DishStatusEnum.Enable)) {
                throw new ServiceException("起售的菜品不可删除");
            }
        });
        // 2. 被套餐关联的菜品不可删除
        Integer setmealCount = setmealDishMapper.countDishId(ids);
        if (setmealCount != null && setmealCount > 0) {
            throw new ServiceException("被套餐关联的菜品不可删除");
        }

        // 3. 删除菜品
        dishMapper.delBatch(ids);

        // 4. 删除口味
        dishFlavorMapper.delBatchByDishId(ids);
    }

    @Override
    public List<DishVO> getByCategoryIdWithUser(DishVO dishVO) {
        List<DishVO> list = dishMapper.select(dishVO.getStatus(), dishVO.getCategoryId().intValue(), "");

        for (DishVO vo : list) {
            List<DishFlavorEntity> dishFlavor = dishFlavorMapper.getByDishId(vo.getId().intValue());
            vo.setFlavors(dishFlavor);
        }
        return list;
    }
}
